﻿using System.Collections;
using System.Configuration;
using System.Data.SqlClient;


namespace MichielWouters
{
    public static class ConnectionClass
    {
        private static SqlConnection conn;
        private static SqlCommand comand;

        static ConnectionClass()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["TesteConnectionString"].ToString();
            conn = new SqlConnection(connectionString);
            comand = new SqlCommand("",conn);
        }

        public static ArrayList GetCoffeeByType(string coffeeType)
        {
            ArrayList list = new ArrayList();
            string query = string.Format("SELECT * FROM coffee WHERE type LIKE '{0}'",coffeeType);

            try
            {
                conn.Open();
                comand.CommandText = query;
                SqlDataReader reader = comand.ExecuteReader();

                while (reader.Read())
                {
                    int id = reader.GetInt32(0);
                    string name = reader.GetString(1);
                    string type = reader.GetString(2);
                    double price = reader.GetDouble(3);
                    string roast = reader.GetString(4);
                    string country = reader.GetString(5);
                    string image = reader.GetString(6);
                    string review = reader.GetString(7);

                    Cafe coffee = new Cafe(id, name, type, price, roast, country, image, review);
                    list.Add(coffee);
                }
            }
            finally
            {
                conn.Close();
            }
            return list;
        }

        public static void AddCoffee(Cafe coffee)
        {
            string query = string.Format(
                @"INSERT INTO coffee VALUES ('{0}','{1}', @prices,'{2}','{3}','{4}','{5}')",
                coffee.Name, coffee.Type, coffee.Roast, coffee.Country,coffee.Image, coffee.Review);
            comand.CommandText = query;
            comand.Parameters.Add(new SqlParameter("@prices", coffee.Price));
            try 
	        {	        
		        conn.Open();
                comand.ExecuteNonQuery();
	        }
	        finally
	        {
		        conn.Close();
                comand.Parameters.Clear();
	        }
        }
    }
}